package com.detech.sboh.base.page;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.constraints.NotNull;

@Api(value="分页查询参数类")
public class PageParam {

    @NotNull(message = "当前页不能为空！")
    @ApiModelProperty(value="当前页",dataType ="int",required = true)
    private int pageNum;

    @NotNull(message = "每页记录数不能为空！")
    @ApiModelProperty(value="每页记录数",dataType ="int",required = true)
    private int pageSize;

    @ApiModelProperty(value = "排序字段",dataType ="string")
    private String orderByColumn;

    @ApiModelProperty(value = "排序的方向", dataType ="string", example = "asc,desc")
    private String orderWise;

    /**
     * 当前记录起始索引 默认值
     */
    public static final int DEFAULT_PAGE_NUM = 1;

    /**
     * 每页显示记录数 默认值 默认查全部
     */
    public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getOrderByColumn() {
        return orderByColumn;
    }

    public void setOrderByColumn(String orderByColumn) {
        this.orderByColumn = orderByColumn;
    }

    public String getOrderWise() {
        return orderWise;
    }

    public void setOrderWise(String orderWise) {
        this.orderWise = orderWise;
    }

    public <T> Page<T> build() {
        Integer pageNum = ObjectUtil.defaultIfNull(getPageNum(), DEFAULT_PAGE_NUM);
        Integer pageSize = ObjectUtil.defaultIfNull(getPageSize(), DEFAULT_PAGE_SIZE);
        if (pageNum <= 0) {
            pageNum = DEFAULT_PAGE_NUM;
        }
        if (pageSize <= 0) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        Page<T> page = new Page(pageNum.toString(), pageSize.toString());
        if (StrUtil.isNotBlank(getOrderByColumn()) && StrUtil.isNotBlank(getOrderWise())) {
            page.setOrderByColumn(getOrderByColumn());
            page.setOrderWise(getOrderWise());
        }
        return page;
    }

}
